var TutorialsPage = Base.extend({
    dontShowTutorialsCheckbox: null,
    updateTutorialsSettingsUrl: null,
    tutorialWidth: null,
    tutorialHeight: null,

    popup: null,

    showTutorialUrl: null,
    openPopupInterval: null,

    constructor: function()
    {
        this.tutorialWidth  = 640;
        this.tutorialHeight = 516;
    
        this.dontShowTutorialsCheckbox = $('#dontShowTutorials');        
        this.dontShowTutorialsCheckbox.bind('check', handler(this, 'dontShowTutorialsCheckboxClickHandler'));  
        this.dontShowTutorialsCheckbox.bind('uncheck', handler(this, 'dontShowTutorialsCheckboxClickHandler'));

        this.updateTutorialsSettingsUrl = $.trim( $('#updateTutorialsSettingsUrl').val() );        
        
        this.initTutorialLinks();
        this.initCheckbox();
    },
    
    initCheckbox: function()
    {
        this.dontShowTutorialsCheckbox.checkbox({cls: 'image-checkbox'});
    },
    
    initTutorialLinks: function()
    {
        var thisPtr = this;
        $( ".tutorial_link" ).click(function(){
            var url = $.trim( $(this).attr('href') );
            if ( url == '#' || url == '' )
                return false;
            thisPtr.openTutorial(url);

            return false;
        });
    },
    
    dontShowTutorialsCheckboxClickHandler: function()
    {
        var checked = this.dontShowTutorialsCheckbox.attr( 'checked' );
        var show = ( !checked ) ? '1' : '0'; 
        this.sendChangeTutorialDisplayingRequest( show );
    },
    
    sendChangeTutorialDisplayingRequest: function(optionVal)
    {
        var data = { 'show': optionVal };
        $.post(this.updateTutorialsSettingsUrl, data);
    },
    
    openTutorial: function(url)
    {
        this.showTutorialUrl = url;

        if ( this.openPopupInterval )
        {
            clearInterval( this.openPopupInterval );
            this.openPopupInterval = null;
        }
        this.openPopupInterval = setTimeout( handler( this, "openTutorialDelayed" ), 500 );
    },

    openTutorialDelayed: function()
    {
        if ( this.showTutorialUrl == null )
        {
            return;
        }

        url = this.showTutorialUrl;

        if ( this.popup )
        {
            this.popup.close();
            this.popup = null;
        }

        var viewOptions = new ViewContentFlashOptions();
        viewOptions.initFlashParams(
            url,
            '',
            this.tutorialWidth,
            this.tutorialHeight,
            'opaque'
        );
        viewOptions.setViewUrl(url);
        this.popup = ViewContentWindow.open('tutorial_popup', viewOptions);        
    }
});

$(function(){
    var page = new TutorialsPage();
});